package org.jumiz.framework.base.reflect;

import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 */
public final class ColumnDefinition {

    private Map<String, ColumnDefinition> fieldNameKeys = new HashMap<>(1);

    private Map<String, ColumnDefinition> columnNameKeys = new HashMap<>(1);

    private String fieldName;

    private String columnName;

    private Class<?> type;

    private ColumnDefinition() {}

    public static ColumnDefinition create() {
        return new ColumnDefinition();
    }

    public ColumnDefinition fieldName(String fieldName) {
        this.fieldName = fieldName;
        return this;
    }

    public ColumnDefinition columnName(String columnName) {
        this.columnName = columnName;
        return this;
    }

    public ColumnDefinition type(Class<?> type) {
        this.type = type;
        return this;
    }

    public void put(String fieldName, String columnName, Class<?> type) {
        ColumnDefinition definition = ColumnDefinition.create().columnName(columnName).fieldName(fieldName).type(type);
        this.fieldNameKeys.put(fieldName, definition);
        this.columnNameKeys.put(columnName, definition);
    }

    public ColumnDefinition getByFieldName(String fieldName) {
        return this.fieldNameKeys.get(fieldName);
    }

    public ColumnDefinition getByColumnName(String columnName) {
        return this.columnNameKeys.get(columnName);
    }

    public Class<?> getType() {
        return type;
    }

    public String getColumnName() {
        return columnName;
    }

    public String getFieldName() {
        return fieldName;
    }

    public String getSqlColumns() {
        return this.columnNameKeys.keySet().stream().collect(Collectors.joining(","));
    }
}
